/** ###################################################################
**     Filename  : Events.c
**     Project   : Modul_libely
**     Processor : MC9S12XDT512BCFU
**     Component : Events
**     Version   : Driver 01.04
**     Compiler  : CodeWarrior HCS12X C Compiler
**     Date/Time : 13. 8. 2012, 8:59
**     Abstract  :
**         This is user's event module.
**         Put your event handler code here.
**     Settings  :
**     Contents  :
**         No public methods
**
** ###################################################################*/
/* MODULE Events */


#include "Cpu.h"
#include "Events.h"

/* User includes (#include below this line is not maintained by Processor Expert) */

#pragma CODE_SEG DEFAULT

/*
** ===================================================================
**     Event       :  TI1_OnInterrupt (module Events)
**
**     Component   :  TI1 [TimerInt]
**     Description :
**         When a timer interrupt occurs this event is called (only
**         when the component is enabled - <Enable> and the events are
**         enabled - <EnableEvent>). This event is enabled only if a
**         <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
extern byte timer_int;

void TI1_OnInterrupt(void)
{
  timer_int = 1;
}

/*
** ===================================================================
**     Event       :  AS1_OnError (module Events)
**
**     Component   :  AS1 [AsynchroSerial]
**     Description :
**         This event is called when a channel error (not the error
**         returned by a given method) occurs. The errors can be read
**         using <GetError> method.
**         The event is available only when the <Interrupt
**         service/event> property is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void AS1_OnError(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  AS1_OnRxChar (module Events)
**
**     Component   :  AS1 [AsynchroSerial]
**     Description :
**         This event is called after a correct character is received.
**         The event is available only when the <Interrupt
**         service/event> property is enabled and either the <Receiver>
**         property is enabled or the <SCI output mode> property (if
**         supported) is set to Single-wire mode.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/


void AS1_OnRxChar(void)
{
  
}

/*
** ===================================================================
**     Event       :  AS1_OnTxChar (module Events)
**
**     Component   :  AS1 [AsynchroSerial]
**     Description :
**         This event is called after a character is transmitted.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void AS1_OnTxChar(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  AS1_OnFullRxBuf (module Events)
**
**     Component   :  AS1 [AsynchroSerial]
**     Description :
**         This event is called when the input buffer is full;
**         i.e. after reception of the last character 
**         that was successfully placed into input buffer.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void AS1_OnFullRxBuf(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  AS1_OnRxCharExt (module Events)
**
**     Component   :  AS1 [AsynchroSerial]
**     Description :
**         This event is called after a correct character is received.
**         The last received character is passed as a parameter of the
**         event function.
**         Nevertheless, the last received character is placed in the
**         external buffer of the component.
**         This event is identical in function with the <OnRxChar>
**         event with a parameter added. It is not recommended to use
**         both <OnRxChar> and OnRxCharExt events at the same time.
**         The event is available only when the <Interrupt
**         service/event> property is enabled and either the <Receiver>
**         property is enabled or the <SCI output mode> property (if
**         supported) is set to Single-wire mode.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Chr             - The last character correctly received.
**     Returns     : Nothing
** ===================================================================
*/

// prerusenie prijateho znaku od libely 1

byte AS1_char;

extern byte libela1_spracuj;
extern byte libela1_j;
extern byte libela1_buf_pocet;
extern char libela1_buf[40]; 

void AS1_OnRxCharExt(AS1_TComData Chr)
{
  // ak je v hlavnom cykle rozparsovane 
  if (libela1_spracuj == 0) {
    //nacitanie poctu prijatych znakov v bufferi
    libela1_buf_pocet = (byte) AS1_GetCharsInRxBuf();

    // pokial nie je vycitany prijimaci buffer
    while ((byte) AS1_GetCharsInRxBuf() > 0) {
      // nacitanie jedneho znaku z prijimacieho buffera
      (void) AS1_RecvChar(&AS1_char);

      // synchronizacia zaciatku spravy v pripade prijateho pismena R
      if (AS1_char == 'R') {
        // nastavenie sa na zaciatok buffera pre parsovanie
        libela1_j = 0; 
        libela1_buf[libela1_j] = (char) AS1_char;
        ++libela1_j;
      } 

      // prikaz na parsovanie v pripade prijateho ukoncenia riadku
      // alebo prekrocenia dlzky buffera pre parsovanie
      else if(AS1_char == 0x0A || libela1_j == 39) {        
        
        libela1_spracuj = 1;
        (void) AS1_ClearRxBuf();
       
      }
      
      // inak prijatie znaku a ulozenie do buffera pre parsovanie
      else {
        
        // zabranenie prepisania pamate za bufferom      
        if (libela1_j >= 39) {
          libela1_j = 38; 
        }

        libela1_buf[libela1_j] = (char) AS1_char;
        ++libela1_j;
      }
    }
  }
}

/*
** ===================================================================
**     Event       :  SM2_OnRxChar (module Events)
**
**     Component   :  SM2 [SynchroMaster]
**     Description :
**         This event is called after a correct character is received.
**         The event is available only when the <Interrupt
**         service/event> property is enabled.
**         
**         [ Version specific information for Freescale HCS08 and RS08
**         and HCS12 and HCS12X and ColdFireV1 derivatives ] 
**         The <Bidirectional mode> property setting doesn't affect
**         this event.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
/*
** ===================================================================
**     Event       :  AS2_OnError (module Events)
**
**     Component   :  AS2 [AsynchroSerial]
**     Description :
**         This event is called when a channel error (not the error
**         returned by a given method) occurs. The errors can be read
**         using <GetError> method.
**         The event is available only when the <Interrupt
**         service/event> property is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void AS2_OnError(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  AS2_OnRxChar (module Events)
**
**     Component   :  AS2 [AsynchroSerial]
**     Description :
**         This event is called after a correct character is received.
**         The event is available only when the <Interrupt
**         service/event> property is enabled and either the <Receiver>
**         property is enabled or the <SCI output mode> property (if
**         supported) is set to Single-wire mode.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void AS2_OnRxChar(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  AS2_OnRxCharExt (module Events)
**
**     Component   :  AS2 [AsynchroSerial]
**     Description :
**         This event is called after a correct character is received.
**         The last received character is passed as a parameter of the
**         event function.
**         Nevertheless, the last received character is placed in the
**         external buffer of the component.
**         This event is identical in function with the <OnRxChar>
**         event with a parameter added. It is not recommended to use
**         both <OnRxChar> and OnRxCharExt events at the same time.
**         The event is available only when the <Interrupt
**         service/event> property is enabled and either the <Receiver>
**         property is enabled or the <SCI output mode> property (if
**         supported) is set to Single-wire mode.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Chr             - The last character correctly received.
**     Returns     : Nothing
** ===================================================================
*/

// prerusenie prijateho znaku od libely 2

byte AS2_char;

extern byte libela2_spracuj;
extern byte libela2_j;
extern byte libela2_buf_pocet;
extern char libela2_buf[40]; 

void AS2_OnRxCharExt(AS2_TComData Chr)
{
 // ak je v hlavnom cykle rozparsovane
 if (libela2_spracuj == 0) {
   //nacitanie poctu prijatych znakov v bufferi
   libela2_buf_pocet = (byte) AS2_GetCharsInRxBuf();

   // pokial nie je vycitany prijimaci buffer
   while ((byte) AS2_GetCharsInRxBuf() > 0) {
     // nacitanie jedneho znaku z prijimacieho buffera
     (void) AS2_RecvChar(&AS2_char);
     
     // synchronizacia zaciatku spravy v pripade prijateho pismena R
     if (AS2_char == 'R') {
        // nastavenie sa na zaciatok buffera pre parsovanie
        libela2_j = 0; 
        libela2_buf[libela2_j] = (char) AS2_char;
        ++libela2_j;
     } 
     
     // prikaz na parsovanie v pripade prijateho ukoncenia riadku
     // alebo prekrocenia dlzky buffera pre parsovanie
     else if(AS2_char == 0x0A || libela2_j == 39) {        
        
        libela2_spracuj = 1;
        (void) AS2_ClearRxBuf();
       
     }
     
     // inak prijatie znaku a ulozenie do buffera pre parsovanie
     else {     
        
        // zabranenie prepisania pamate za bufferom
        if (libela2_j >= 39) {
          libela2_j = 38; 
        }
    
        libela2_buf[libela2_j] = (char) AS2_char;
        ++libela2_j;
     }
   }
 }
}


/*
** ===================================================================
**     Event       :  AS2_OnTxChar (module Events)
**
**     Component   :  AS2 [AsynchroSerial]
**     Description :
**         This event is called after a character is transmitted.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void AS2_OnTxChar(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  AS2_OnFullRxBuf (module Events)
**
**     Component   :  AS2 [AsynchroSerial]
**     Description :
**         This event is called when the input buffer is full;
**         i.e. after reception of the last character 
**         that was successfully placed into input buffer.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void AS2_OnFullRxBuf(void)
{
  /* Write your code here ... */
}


